﻿using System.Text;
using System.Data;
using System.Data.OracleClient;

namespace VirtualAccess.Devices.Oracle.Native
{
    public class NativeOracleDataProvider : CustomOracleDataProvider, IDataProvider
    {
        private OracleConnection _connection = null;

        public NativeOracleDataProvider()
        {
        }

        public NativeOracleDataProvider(string dataSource, string userName, string password)
        {
            this.DataSource = dataSource;
            this.UserName = userName;
            this.Password = password;
        }

        public bool IsOpened
        {
            get
            {
                return _connection.State == System.Data.ConnectionState.Open;
            }
        }

        public void Open()
        {
            if (_connection == null)
            {
                _connection = new OracleConnection();
            }
            if (_connection.State == ConnectionState.Closed)
            {
                try
                {
                    string connectionString = "user id=" + _userName + ";data source=" + _dataSource + ";password=" + _password + "";
                    _connection.ConnectionString = connectionString;
                    _connection.Open();
                }
                catch
                {
                    //不处理。
                }
            }
        }

        public void Close()
        {
            if (_connection != null && _connection.State != System.Data.ConnectionState.Connecting)
            {
                try
                {
                    _connection.Close();
                }
                catch
                {
                    //不处理。
                }
                _connection.Dispose();
                _connection = null;
            }
        }

        public SelectCommand CreateSelectCommand(DataSource dataSource)
        {
            SelectCommand command = new SelectCommand(this);
            if (dataSource is TableDataSource)
            {
                command.CommandText = "SELECT * FROM " + ((TableDataSource)dataSource).TableName;
            }
            else if (dataSource is SelectDataSource)
            {
                command.CommandText = GetSelectStatement(dataSource);
            }
            return command;
        }

        public SimpleUpdateCommand CreateSimpleUpdateCommand()
        {
            return new SimpleUpdateCommand(this);
        }

        private string GetSelectStatement(DataSource dataSource)
        {
            string commandText = string.Empty;
            if (dataSource is TableDataSource)
            {
                commandText = ((TableDataSource)dataSource).TableName;
            }
            else if (dataSource is SelectDataSource)
            {
                SelectDataSource selectDataSource = (SelectDataSource)dataSource;
                StringBuilder buffer = new StringBuilder();
                buffer.AppendLine("SELECT * FROM (" + GetSelectStatement(selectDataSource.SourceDataSource) + ") " + dataSource.AliasName);
                if (selectDataSource.Filter != null)
                {
                    buffer.AppendLine("WHERE " + GetWhereClause(selectDataSource.Filter));
                }
                if (selectDataSource.Groupper != null)
                {
                    buffer.AppendLine("GROUP BY " + GetGroupByClause(selectDataSource.Groupper));
                }
                if (selectDataSource.Order != null)
                {
                    buffer.AppendLine("ORDER BY " + GetOrderByClause(selectDataSource.Order));
                }
                commandText = buffer.ToString();

            }
            return commandText;
        }

        private string GetWhereClause(DataSourceFilter filter)
        {
            return string.Empty;
        }

        private string GetGroupByClause(DataSourceStatisticsOperation groupper)
        {
            return string.Empty;
        }

        private string GetOrderByClause(DataSourceOrder order)
        {
            return string.Empty;
        }

        public IDbCommand CreateLocalCommand()
        {
            return new OracleDataCommand(string.Empty, _connection);
        }

        public IDataReader CreateDataReader(IDataReader localDataReader)
        {
            return localDataReader;
        }
    }
}
